#!/usr/bin/env python
# coding: utf-8

# In[6]:


# -*- coding: utf-8 -*-
"""
特征删除与数据更新流程（最终修正版）
"""

#%% 模块一：依赖导入
import pandas as pd
import os

#%% 模块二：原始数据清洗（保持不变）
def clean_processed_data(input_path, output_path, drop_columns):
    df = pd.read_csv(input_path)
    missing_cols = [col for col in drop_columns if col not in df.columns]
    if missing_cols:
        raise ValueError(f"以下列不存在于原始数据：{missing_cols}")
    cleaned_df = df.drop(columns=drop_columns)
    cleaned_df.to_csv(output_path, index=False)
    print(f"原始数据清洗完成，删除列：{drop_columns}")
    print(f"新维度：{cleaned_df.shape}，保存路径：{os.path.abspath(output_path)}")
    return cleaned_df

#%% 模块三：编码数据清洗（升级版）
def clean_encoded_data(input_path, output_path, drop_columns):
    """处理编码后数据（按列名删除）"""
    df = pd.read_csv(input_path)

    # 检查列名格式是否匹配
    actual_cols = df.columns.tolist()
    missing_cols = [col for col in drop_columns if col not in actual_cols]

    if missing_cols:
        # 尝试自动转换列名格式
        converted_cols = [col.lower().replace(':', '_') for col in drop_columns]
        missing_cols = [col for col in converted_cols if col not in actual_cols]
        if missing_cols:
            raise ValueError(f"以下列不存在（含转换后）：{missing_cols}")
        drop_columns = converted_cols

    # 执行删除
    cleaned_df = df.drop(columns=drop_columns)
    cleaned_df.to_csv(output_path, index=False)
    print(f"编码数据清洗完成，删除列：{drop_columns}")
    print(f"新维度：{cleaned_df.shape}，保存路径：{os.path.abspath(output_path)}")
    return cleaned_df

#%% 主流程
if __name__ == '__main__':
    # 配置参数
    processed_input = 'processed_data.csv'
    encoded_input = 'encoded_data.csv'
    processed_output = 'processed_data_cleaned.csv'
    encoded_output = 'encoded_data_cleaned.csv'

    # 原始数据删除列（保持原始列名）
    processed_drop_columns = [
        'telephone', 
        'foreign_worker',
        'present_residence_since',
        'installment_rate',
        'other_debtors_guarantors',
        'number_of_people_being_liable',
        'duration_in_month_binned',
        'credit_amount_binned',
        'age_in_years_binned',
        'installment_rate_binned',
        'present_residence_since_binned',
        'number_of_existing_credits_binned',
        'number_of_people_being_liable_binned'
    ]

    # 编码数据删除列（适配转换后列名）
    encoded_drop_columns = [
        'present_residence_since',
        'installment_rate',
        'other_debtors_guarantors:A101',  # 注意冒号转为下划线
        'other_debtors_guarantors:A102',
        'other_debtors_guarantors:A103',
        'number_of_people_being_liable',
        'telephone:A191',
        'telephone:A192',
        'foreign_worker:A201',
        'foreign_worker:A202'
    ]

    try:
        # 步骤1：清洗原始数据
        print("\n" + "="*40)
        print("正在处理原始数据...")
        processed_cleaned = clean_processed_data(
            processed_input, 
            processed_output,
            processed_drop_columns
        )

        # 步骤2：清洗编码数据
        print("\n" + "="*40)
        print("正在处理编码数据...")
        encoded_cleaned = clean_encoded_data(
            encoded_input,
            encoded_output,
            encoded_drop_columns
        )

        # 步骤3：验证数据一致性
        print("\n" + "="*40)
        print("数据一致性验证：")

        # 原始数据验证
        orig_processed_cols = pd.read_csv(processed_input).columns.tolist()
        print(f"原始数据列减少：{len(orig_processed_cols)} → {processed_cleaned.shape[1]}")
        assert all(col not in processed_cleaned.columns for col in processed_drop_columns)

        # 编码数据验证
        orig_encoded_cols = pd.read_csv(encoded_input).columns.tolist()
        print(f"编码数据列减少：{len(orig_encoded_cols)} → {encoded_cleaned.shape[1]}")
        assert all(col not in encoded_cleaned.columns for col in encoded_drop_columns)

        print("\n数据清洗验证通过！")

    except Exception as e:
        print("\n操作失败：", str(e))


# In[ ]:




